import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children:[
      {
        path: '/Home/SearchPopup',
        name: 'SearchPopup',
        component: () => import(/* webpackChunkName: "SearchPopup" */ '../views/SearchPopup.vue')
      }
    ]
  },
  {
    path:'/template',
    name:"template",
    component: () => import(/* webpackChunkName: "template" */ '../views/Template.vue'),
    meta:{
       num:1
    }
  },
  {
    path:'/type',
    name:"type",
    component: () => import(/* webpackChunkName: "fenlei" */ '../views/Type.vue'),
    meta:{
      num:2
    },
    children:[
      {
        path:'/Type/SearchPopup',
        name:'SearchPopup',
        component:()=>import((/* webpackChunkName: "SearchPopup" */ '../views/SearchPopup.vue'))
      }
    ]
  },
  {
    path:'/car',
    name:"car",
    component: () => import(/* webpackChunkName: "car" */ '../views/Car.vue'),
    meta:{
       num:3
    }
  },
  {
    path:'/me',
    name:"me",
    component: () => import(/* webpackChunkName: "template" */ '../views/Me.vue'),
    meta:{
       num:4
    }
  },
  {
    path:'/brand',
    name:"brand",
    component: () => import(/* webpackChunkName: "template" */ '../views/Brand.vue'),
  },
  {
    path:'/channel',
    name:"channel",
    component: () => import(/* webpackChunkName: "template" */ '../views/Channel.vue'),
  },
  {
    path:'/details',
    name:"details",
    component: () => import(/* webpackChunkName: "template" */ '../views/Details.vue'),
  },
  {
    path:'/detail',
    name:"detail",
    component: () => import(/* webpackChunkName: "template" */ '../views/Detail.vue'),
  }
]
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
     if(to.path=="/car"){
         if(localStorage.getItem('token')){
           next()
        }else{
          setTimeout(()=>{
            next('/me')
         },1000)
        }
     }
     next()
})
export default router
